package com.faiz.picturebed.exception;

import com.faiz.picturebed.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //全局异常处理

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public R error(Exception e) {
        e.printStackTrace();
        return R.error().message("文件IO异常");
    }

    //自定义异常
    @ExceptionHandler(FileTypeNotSupportException.class)
    @ResponseBody
    public R error(FileTypeNotSupportException e) {
        e.printStackTrace();
        //将日志写到文件中
        log.error(e.getMessage());
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}
